#include "qgradienttool.h"
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneContextMenuEvent>
#include <QBrush>
#include "qgradientdialog.h"
QGradientTool::QGradientTool(QObject *parent)
	: QMyTool(parent),start(),end(),gradient()
{
	begin_edit=false;
}

QGradientTool::~QGradientTool()
{

}

void QGradientTool::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
	QGraphicsItem * tp=Container->mouseGrabberItem();
	if(tp&&(tp->type()==QGraphicsRectItem::Type
		||tp->type()==QGraphicsEllipseItem::Type
		||tp->type()==QGraphicsPathItem::Type
		||controller->GetNodeType(Container->ItemIndex(tp))==WordShape))
	{
		QGradientDialog * dlg=new QGradientDialog;
		dlg->exec();
		if(dlg->IsOK())
		{
			gradient=dlg->Result();
			target=tp;
			begin_edit=true;
		}
	}
}
void QGradientTool::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
}
void QGradientTool::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
	if(mouseEvent->button()==Qt::RightButton)
	{
		gradient=QLinearGradient();
		target=0;
		begin_edit=false;
	}
	else if(begin_edit)
	{
		start=target->mapFromScene(mouseEvent->scenePos());
	}
}
void QGradientTool::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
	if(begin_edit)
	{
		end=target->mapFromScene(mouseEvent->scenePos());
		gradient.setStart(start);
		gradient.setFinalStop(end);
		((QAbstractGraphicsShapeItem*)target)->setBrush(QBrush(gradient));
		int i=Container->ItemIndex(target);
		((ShapeNode*)(controller->GetObjectByIndex(i)))->FillBrushRef()=QBrush(gradient);
		((ShapeNode*)(controller->GetObjectByIndex(-1)))->SetDirty();
		((ShapeNode*)(controller->GetObjectByIndex(-1)))->UpdateCache();
		gradient=QLinearGradient();
		target=0;
		begin_edit=false;
	}
}
void QGradientTool::contextMenuEvent(QGraphicsSceneContextMenuEvent * mouseEvent)
{
}